package com.example.mobileshop;

import java.util.ArrayList;
import java.util.List;

import com.example.mobileshop.controller.ShopController;
import com.example.mobileshop.models.Shop;
import com.example.mobileshop.utils.Const;
import com.example.mobileshop.utils.ShopListAdapter;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class ShopsFragment extends Fragment{	
	//Listview for shops
	private ListView lv1; 
	private boolean flag_loading = false;
	private int currentPage = 0;
	private List<Shop> listShop = new ArrayList<Shop>();	
	private ShopListAdapter customAdapter;
	final public static String BUNDLE_KEY_LOGIN = "login";
	final public static String BUNDLE_KEY_PRE_FRAGMENT = "fragment_name";
	
	private ShopsFragment(){}
	
	private static ShopsFragment instance = new ShopsFragment();
	
	public static ShopsFragment instance(){
		return instance; 
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_shops, container, false);        
        return rootView;
    }
	
	//@Override
	//public void onResume(){		
	//	super.onResume();
	//	Bundle bundle = this.getArguments();
	//	String loginResponse = bundle.getString(BUNDLE_KEY_LOGIN);
	//	String previousFragmentTitle = bundle.getString(BUNDLE_KEY_PRE_FRAGMENT);
	//	if(previousFragmentTitle.equalsIgnoreCase(getResources().getString(R.string.SignIn_screen))){
			
	//	}
	//}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);	
		//Set up listview
		lv1 = (ListView) getActivity().findViewById(R.id.list_view);			
		customAdapter = new ShopListAdapter(getActivity(), listShop);
		lv1.setAdapter(customAdapter);					
		lv1.setOnScrollListener(new OnScrollListener() {
            public void onScrollStateChanged(AbsListView view, int scrollState) {


            }

            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {

                if(firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount!=0)
                {
                    if(flag_loading == false)
                    {
                    	currentPage++;
                        flag_loading = true;
                        new GetShops().execute(currentPage + 1);                     
                    }
                }
            }
        });		
		new GetShops().execute(currentPage + 1);
	}		
	
	/**
     * Async task class to get json by making HTTP call
     * */
    private class GetShops extends AsyncTask<Integer, Void, Void> {    	    	    	     	    	
    	@Override
        protected void onPreExecute() {
            super.onPreExecute();             
        }

		@Override
		protected Void doInBackground(Integer... arg0) {
			ShopController shopController = new ShopController();
			List<Shop> lst = shopController.getShopsForAPage(arg0[0]);
			if (lst != null && lst.size() == Const.NUM_OF_SHOPS_ON_PAGE){
				flag_loading = false;
			}
			listShop.addAll(lst);		
			return null;
		}
		
		@Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);        
           customAdapter.notifyDataSetChanged();
        }    	
    }
}
